package com.yxjx.duoxue.datamodel;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class DMBaiduLocation extends DMObject implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6324287553904483111L;
	private String time;
	private int locType;
	private double latitude;
	private double longtitude;
	private double radius;
	private String city;
	private String district;
	private String addrStr;
	
	@Override
	public String toString() {
		JSONObject jo = new JSONObject();
		try {
			jo.put("city", getCity());
			jo.put("district", getDistrict());
			jo.put("latitude", getLatitude());
			jo.put("locType", getLocType());
			jo.put("lontitude", getLongtitude());
//			jo.put("radius", getRadius());
			jo.put("time", getTime());
			jo.put("addrStr", getAddrStr());
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
		
		return jo.toString();
	};
	
	public static DMBaiduLocation from(String str) {
		if (UIHelper.isEmpty(str)) {
			return null;
		}
		try {
			DMBaiduLocation location = new DMBaiduLocation();
			JSONObject jo = new JSONObject(str);
			location.setCity(getString(jo, "city"));
			location.setDistrict(getString(jo, "district"));
			location.setLatitude(getDouble(jo, "latitude"));
			location.setLocType(getInteger(jo, "locType"));
			location.setLongtitude(getDouble(jo, "lontitude"));
//			location.setRadius((Double) jo.get("radius"));
			location.setTime(getString(jo, "time"));
			location.setAddrStr(getString(jo, "addrStr"));
			return location;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 将该条位置信息保存到SharedPreference
	 * @param ctx
	 */
	public void saveToSharedPreference(Context ctx) {
		Utils.writeToSharePreference(ctx, Yx.CURRENT_LOCATION, this.toString());
	}
	
	/**
	 * 获取当前的位置信息
	 * 首先尝试从内存中读取，失败则从SharedPreference读取
	 * @param ctx
	 * @return
	 */
	public static DMBaiduLocation getLocation(Context ctx) {
		
		DMBaiduLocation location = Globals.getInstance(ctx).getLocation();
		if (location == null) {
			String str = Utils.getValueFromSharedPreference(ctx, Yx.CURRENT_LOCATION);
			location = DMBaiduLocation.from(str);
			Globals.getInstance(ctx).setLocation(location);
		}
		return location;
	}
	
	/**
	 * force to get location info from sharedPreference, used when re-location.
	 * @param ctx
	 * @return
	 */
	public static DMBaiduLocation forceGetLocationFromSharedPreference(
			Context ctx) {
		String str = Utils.getValueFromSharedPreference(ctx,
				Yx.CURRENT_LOCATION);
		Utils.logd("CMM7 location str = " + str);
		DMBaiduLocation location = DMBaiduLocation.from(str);
		Globals.getInstance(ctx).setLocation(location);
		return location;
	}
	
	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getLocType() {
		return locType;
	}

	public void setLocType(int locType) {
		this.locType = locType;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongtitude() {
		return longtitude;
	}

	public void setLongtitude(double longtitude) {
		this.longtitude = longtitude;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getAddrStr() {
		return addrStr;
	}

	public void setAddrStr(String addrStr) {
		this.addrStr = addrStr;
	}

}
